<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      canvas {
        outline: 1px solid red;
      }
    </style>
  </head>
  <body>
    <canvas width="500" height="500" id="canvas"></canvas>
    <script>
      let canvas = document.querySelector("#canvas");
      let context = canvas.getContext("2d");

      let dot = {
        x: 20,
        y: 100,
        r: 5,
        speedX: 0.5,
        speedY: 0,
        color: "orange",
      };

      const draw = function (dot) {
        dot.speedY += 0.1;
        dot.x += dot.speedX;
        dot.y += dot.speedY;

        // if (dot.x >= 490 - dot.r || dot.x <= 10) {
        //   dot.speedX = -dot.speedX;
        // }
        if (dot.y >= 495 || dot.y <= 5) {
          dot.speedY = -dot.speedY * 0.9;

          console.log(dot.speedY);
        }
      };
      const update = function (dot) {
        context.beginPath();
        context.arc(dot.x, dot.y, dot.r, 0, 2 * Math.PI);
        context.fillStyle = dot.color;
        context.fill();
      };

      setInterval(() => {
        context.clearRect(0, 0, 500, 500);

        update(dot);
        draw(dot);
      }, 1000 / 60);
    </script>
  </body>
</html>
